//package com.sxbbc.common.core.service.system.pay;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.sxbbc.common.basics.exceptions.ThrowJsonException;
//import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
//import com.sxbbc.common.basics.utils.date.DateUtil;
//import com.sxbbc.common.basics.utils.statics.StaticUtils;
//import com.sxbbc.common.core.entity.*;
//import com.sxbbc.common.core.enums.*;
//import com.sxbbc.common.core.enums.redis.RedisExpiredEnums;
//import com.sxbbc.common.core.service.system.pay.dto.SettleDetailDto;
//import com.sxbbc.common.core.service.system.pay.dto.SettleDto;
//import org.apache.commons.lang3.StringUtils;
//
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * <p>
// *
// * </p>
// *
// * @author zhangq
// * @since 2020/4/11
// */
//public class Test {
//
//    public BigDecimal handleSingleOrderPay(SettleDto settleDto, Integer cid, String payCode,
//                                           Integer payType, String remark, Integer fromChannel, Integer payChannel) {
//        //处理生成订单信息
//        String orderCode = CalcuUtils.getInstance().getOrderCode("O");
//        OrderMain orderMain = new OrderMain();
//        orderMain.setCid(cid);
//        orderMain.setBid(settleDto.getBid());
//        orderMain.setbType(settleDto.getbType());
//        //orderMain.setWid(settleDto.getWarehouseId());
//        orderMain.setPayCode(payCode);
//        orderMain.setOrderCode(orderCode);
//        orderMain.setTotalNum(settleDto.getTotalNum());
//        orderMain.setGoodsMoney(settleDto.getGoodsMoney());
//        orderMain.setRealMoney(settleDto.getRealMoney());
//        orderMain.setExpMoney(settleDto.getExpMoney());
//        orderMain.setPayMoney(settleDto.getPayMoney());
//        orderMain.setDiscountMoney(settleDto.getDiscountMoney());
//        orderMain.setCouponMoney(settleDto.getCouponMoney());
//        orderMain.setOtherMoney(settleDto.getOtherMoney());
//        orderMain.setPoint(settleDto.getPoint());
//        orderMain.setOrderType(settleDto.getOrderType());
//        orderMain.setLabel(settleDto.getLabel());
//        orderMain.setOrderStatus(OrderStatusEnums.COMFIRE.getId());
//        orderMain.setPayType(payType);
//        //计算支付截止时间
//        QueryWrapper<InfoShopping> wrapper = new QueryWrapper<>();
//        InfoShopping infoShopping = iInfoShoppingService.getOne(wrapper);
//        Date confirmTime = DateUtil.getInstance().currentDate();
//        orderMain.setConfirmTime(confirmTime);
//        Date payEndTime = DateUtil.getInstance().addMinute(confirmTime, infoShopping.getAutoCancel());
//        orderMain.setPayendTime(payEndTime);
//        orderMain.setRemark(remark);
//        orderMain.setFromChannel(fromChannel);
//        orderMain.setPayChannel(payChannel);
//        //保存订单信息
//        boolean flag = iOrderMainService.save(orderMain);
//        if (!flag) {
//            throw new ThrowJsonException("订单生成失败CODE:1001");
//        }
//        //保存发货单
//        CReceivedAddr addr = settleDto.getReceivedAddr();
//        //如果是团购
//        if (CalcuUtils.getInstance().isEquals(OrderTypeEnums.GROUPS.getId(), settleDto.getOrderType())) {
//            if (!StringUtils.isEmpty(settleDto.getGroupcode())) {
//                //如果是参与别人的平团 查询开团信息
//                QueryWrapper<OrderConfigGroup> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("group_code", settleDto.getGroupcode());
//                queryWrapper.eq("leader", StaticUtils.STATUS_YES);
//                OrderConfigGroup configGroup = iOrderConfigGroupService.getOne(queryWrapper);
//                //校验团购信息
//                validGroup(configGroup);
//                //新生成一个团购订单
//                configGroup.setId(null);
//                configGroup.setCid(cid);
//                configGroup.setLeader(StaticUtils.STATUS_NO);
//                configGroup.setStatus(GroupStatusEnums.GROUP_CREATE.getId());
//                configGroup.setOrderCode(orderCode);
//                iOrderConfigGroupService.save(configGroup);
//            }
//        }
//
//        List<SettleDetailDto> detailDtos = settleDto.getDetailDtos();
//        Map<String, String> map = new HashMap<>();
//        for (SettleDetailDto d : detailDtos) {
//            //如果是团购  团购只可能是单个订单单个商品 这里只会存在一个数据
//            if (CalcuUtils.getInstance().isEquals(OrderTypeEnums.GROUPS.getId(), settleDto.getOrderType())) {
//                if (StringUtils.isEmpty(settleDto.getGroupcode())) {
//                    //如果是自己开团
//                    OrderConfigGroup configGroup = new OrderConfigGroup();
//                    //新生成一个团购订单
//                    String groupCode = CalcuUtils.getInstance().getOrderCode("G");
//                    configGroup.setGroupCode(groupCode);
//                    configGroup.setCid(cid);
//                    configGroup.setLeader(StaticUtils.STATUS_YES);
//                    configGroup.setStatus(GroupStatusEnums.GROUP_CREATE.getId());
//                    configGroup.setOrderCode(orderCode);
//                    configGroup.setGroupNum(d.getGroupNum());
//                    configGroup.setGroupTime(d.getGroupTime());
//                    configGroup.setOverTime(d.getOverTime());
//                    iOrderConfigGroupService.save(configGroup);
//                    //设置团购编号到实体
//                    settleDto.setGroupcode(groupCode);
//                }
//            }
//            //获取仓库id
//            Integer wid = iwWarehouseService.selectWarehouseIdByAddr(addr.getId(), d.getProcode(), d.getSkuid(), d.getQty());
//            if (wid == null) {
//                wid = -1;
//            }
//            String mKey = String.valueOf(wid);
//            String mVal = map.get(mKey);
//            if (!StringUtils.isEmpty(mVal)) {
//                map.put(mKey, mVal + "," + d.getSkuid());
//            } else {
//                map.put(mKey, String.valueOf(d.getSkuid()));
//            }
//            OrderDetail detail = new OrderDetail();
//            detail.setOrderCode(orderCode);
//            detail.setProId(d.getPid());
//            detail.setProCode(d.getProcode());
//            detail.setProDetailId(d.getSkuid());
//            detail.setProName(d.getPname());
//            detail.setProSpecName(d.getSkuname());
//            detail.setProUrl(d.getPurl());
//            detail.setProUnit(d.getUnit());
//            detail.setQty(d.getQty());
//            detail.setWeight(d.getWeight());
//            detail.setPrice(d.getPrice());
//            detail.setGoodMoney(d.getGoodmoney());
//            detail.setDiscountMoney(d.getDiscountMoney());
//            detail.setCouponMoney(d.getCouponMoney());
//            detail.setOtherMoney(d.getOtherMoney());
//            detail.setPoint(d.getPoint());
//            detail.setLabel(d.getLabel());
//            detail.setActivitySign(d.getActivitySign());
//            detail.setProType(d.getProType());
//            detail.setCostPrice(d.getCostPrice());
//            detail.setIsRefund(d.getRefundFlag());
//            //处理扣除库存 如果不是秒杀库存 商品库存 如果是秒杀增加秒杀已售库存
//            if (!ActivitySignEnums.SECKILL.getName().equals(d.getActivitySign())) {
//                ProSkuDetail sku = iProSkuDetailService.getById(d.getSkuid());
//                sku.setId(d.getSkuid());
//                sku.setOperationType(OperationTypeEnums.SUB_QTY.getId());
//                sku.setQty(d.getQty());
//                int op = iProSkuDetailService.updateQtyVersion(sku);
//                if (op < 1) {
//                    throw new ThrowJsonException("商品【" + d.getPname() + "】库存不足~");
//                }
//            }
//            //保存订单明细表
//            boolean dFlag = iOrderDetailService.save(detail);
//            if (!dFlag) {
//                throw new ThrowJsonException("订单生成失败CODE:1002");
//            }
//            //如果当前存在 秒杀信息
//            if (d.getOrderSeckillConfig() != null) {
//                OrderSeckillConfig config = d.getOrderSeckillConfig();
//                config.setOrderCode(orderCode);
//                config.setCid(cid);
//                config.setDetailid(detail.getId());
//                iOrderSeckillConfigService.save(config);
//                //操作秒杀库存 增加秒杀已售数量
//                ActivitySeckillDetail activitySeckillDetail = iActivitySeckillDetailService.getById(config.getSkDetailid());
//                if (activitySeckillDetail != null) {
//                    activitySeckillDetail.setBuyNum(activitySeckillDetail.getBuyNum() + config.getQty());
//                    if (activitySeckillDetail.getBuyNum() > activitySeckillDetail.getTotalNum()) {
//                        throw new ThrowJsonException("商品【" + d.getPname() + "】活动库存不足,请重新选购~");
//                    }else if(activitySeckillDetail.getLimitNum() != 0 && config.getQty() > activitySeckillDetail.getLimitNum()){
//                        throw new ThrowJsonException("商品【" + d.getPname() + "】每人限购数量为 " + activitySeckillDetail.getLimitNum() + "件");
//                    }
//                    iActivitySeckillDetailService.updateById(activitySeckillDetail);
//                }
//            }
//        }
//        //将订单号保存到实体
//        settleDto.setCid(cid);
//        settleDto.setOrdercode(orderCode);
//
//        //保存发货单同一个仓库为一个发货单
//        for (String w : map.keySet()) {
//            Integer wid = Integer.parseInt(w);
//            if ("-1".equals(w)) {
//                wid = null;
//            }
//            String detailIds = map.get(w);
//            OrderExpDetail expDetail = new OrderExpDetail();
//            expDetail.setBid(orderMain.getBid());
//            expDetail.setWid(wid);
//            expDetail.setCid(cid);
//            expDetail.setDetailIds(detailIds);
//            expDetail.setbType(orderMain.getbType());
//            expDetail.setOrderCode(orderCode);
//            expDetail.setAddrids(addr.getProId() + "," + addr.getCityId() + "," + addr.getAreaId());
//            expDetail.setAddressDetail(addr.getAddress());
//            expDetail.setAddrPrefix(addr.getNameExt());
//            expDetail.setReceiver(addr.getName());
//            expDetail.setPhone(addr.getPhone());
//            iOrderExpDetailService.save(expDetail);
//        }
//
//        redisUtilsService.setKey(RedisExpiredEnums.ORDER_BUY.getKey() + orderCode, JSONObject.toJSONString(settleDto), RedisExpiredEnums.ORDER_BUY.getSecond());
//        return settleDto.getPayMoney();
//    }
//}
